package com.fs.web;

import com.alibaba.fastjson.JSONObject;
import com.fs.pojo.Notice;
import com.fs.service.NoticeService;
import com.fs.utils.BeanFactories;
import com.fs.utils.JsonData;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/notice/*")
public class NoticeServlet extends HttpServlet {

    private NoticeService noticeService = BeanFactories.getBean(NoticeService.class);
    private HttpServletRequest req;
    private HttpServletResponse resp;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        this.req = req;
        this.resp = resp;
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String pathInfo = req.getPathInfo();
        switch (pathInfo){
            case "/queryCourseNotice":
                queryCourseNotice();
                break;
            case "/deleteNoticeById":
                deleteNoticeById();
                break;
            case "/seacherByTitle":
                seacherByTitle();
                break;
            case "/addNotice":
                addNotice();
                break;
        }
    }

    private void addNotice() throws IOException {
        Notice notice = JSONObject.parseObject(IOUtils.toString(req.getInputStream(), "utf-8"), Notice.class);
        int i =  noticeService.addNotice(notice);
        if (i>0){
            resp.getWriter().write(JSONObject.toJSONString(new JsonData(1,"","发布成功!")) );
        }else {
            resp.getWriter().write(JSONObject.toJSONString(new JsonData(0,"","发布失败!")));
        }
    }

    private void seacherByTitle() throws IOException {
        String title = req.getParameter("title");
        String classId = req.getParameter("classId");
        List<Notice> list= noticeService.getNoticeByTitle(title,classId);

        resp.getWriter().write(JSONObject.toJSONString( JsonData.buildSuccess(list)));
    }

    private void deleteNoticeById() throws IOException {
        String id = req.getParameter("id");
        int i = noticeService.deleteById(id);
        JsonData data;
        if (i>0){
            data = new JsonData(1,"","删除成功!");
        }else {
            data = new JsonData(0,"","删除失败!!");
        }

        resp.getWriter().write(JSONObject.toJSONString(data));
    }

    private void queryCourseNotice() throws IOException {
        String classId = req.getParameter("classId");
        List<Notice> noticeList =noticeService.getAllNoticeOfClass(classId);
        JsonData data = JsonData.buildSuccess(noticeList);
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print(JSONObject.toJSONString(data));

    }

}
